#include "FlyFactoryStatic.h"
#include <QDebug>

#include "UI/ModRun/Output/FactoryOutput.h"


FlyFactoryStatic::FlyFactoryStatic()
{
    QString strOutput = "单例静态工厂模式:构造";
    qDebug() << strOutput;
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateRunOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outList{ strOutput };
        pOutPut->OutPut(outList);
    }

    m_mapFly.clear();
}

FlyFactoryStatic::~FlyFactoryStatic()
{
    QString strOutput = "单例静态工厂模式:析构";
    qDebug() << strOutput;
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateRunOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outList{ strOutput };
        pOutPut->OutPut(outList);
    }

    m_mapFly.clear();
}

//获取单例;
FlyFactoryStatic* FlyFactoryStatic::GetInstance()
{
    static FlyFactoryStatic single;
    return &single;
}

/// <summary>
/// 获取一个FlyCom;
/// </summary>
std::shared_ptr<FlyComponent> FlyFactoryStatic::GetFlyCom(QString strName)
{
    QString strThis = QString::number(reinterpret_cast<quintptr>(this), 16);
    QString strOutput = "单例静态工厂模式:GetFlyCom " + strThis;
    qDebug() << strOutput;
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateRunOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outList{ strOutput };
        pOutPut->OutPut(outList);
    }

    if (strName.isEmpty())
    {
        return nullptr;
    }

    auto iter = m_mapFly.find(strName);
    if (iter != m_mapFly.end())
    {
        return m_mapFly[strName];
    }

    //没有就重新创建并返回;
    std::shared_ptr<FlyComponent> comp(new FlyComponent(strName));
    m_mapFly[strName] = comp;
    return comp;
}

/// <summary>
/// 打印对象;
/// </summary>
void FlyFactoryStatic::Display()
{
    QString strThis = QString::number(reinterpret_cast<quintptr>(this), 16);
    QString strOutput = "单例静态工厂模式:Display " + strThis;
    qDebug() << strOutput;
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateRunOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outList{ strOutput };
        pOutPut->OutPut(outList);
    }

    for (auto iter = m_mapFly.begin(); iter != m_mapFly.end(); iter++)
    {
        std::shared_ptr<FlyComponent> ret = (iter->second);
        if (ret)
        {
            ret->Operation();
        }
    }
}
